/**
* @File:mp3player_example.c
* @Version:V1.0
* @Author:rudius
* @Brief: MP3播放器模块测试函数
* @Logs:
* 	Date		Author		Notes
* 	2025-07-14	rudius		first version
*/

#include "mp3player.h"
#include <rtthread.h>
#include <rtdevice.h>
#include <stdlib.h>

#ifdef FINSH_USING_MSH
#include <finsh.h>
#endif

#define TEST_FILE_PATH "/sdcard/test.mp3"
#define PLAY_TIME_MS 5000  // 测试播放时间（毫秒）

static struct mp3_player test_player;

static void mp3_player_test_thread_entry(void *parameter)
{
    rt_kprintf("\n[MP3 Player Test] Starting...\n");
    
    // 1. 初始化播放器
    rt_kprintf("Initializing MP3 player...\n");
    int res = mp3_player_init(&test_player);
    if (res != RT_EOK) {
        rt_kprintf("Player initialization failed! Error: %d\n", res);
        return;
    }
    
    // 2. 打开MP3文件
    rt_kprintf("Opening MP3 file: %s\n", TEST_FILE_PATH);
    res = mp3_player_open(&test_player, TEST_FILE_PATH);
    if (res != RT_EOK) {
        rt_kprintf("File open failed! Error: %d\n", res);
        return;
    }
    
    // 3. 开始播放
    rt_kprintf("Starting playback...\n");
    mp3_player_play(&test_player);
    
    // 4. 播放一段时间
    rt_thread_mdelay(PLAY_TIME_MS);
    
    // 5. 暂停播放
    rt_kprintf("Pausing playback...\n");
    mp3_player_pause(&test_player);
    
    // 6. 等待1秒
    rt_thread_mdelay(1000);
    
    // 7. 恢复播放
    rt_kprintf("Resuming playback...\n");
    mp3_player_play(&test_player);
    
    // 8. 播放一段时间
    rt_thread_mdelay(PLAY_TIME_MS);
    
    // 9. 设置音量（50%）
    rt_kprintf("Setting volume to 50%%...\n");
    mp3_player_set_volume(&test_player, 50);
    
    // 10. 播放一段时间
    rt_thread_mdelay(PLAY_TIME_MS);
    
    // 11. 停止播放
    rt_kprintf("Stopping playback...\n");
    mp3_player_stop(&test_player);
    
    // 12. 释放资源
    rt_kprintf("Releasing player resources...\n");
    mp3_player_deinit(&test_player);
    
    rt_kprintf("\n[MP3 Player Test] Completed successfully!\n");
}

// Finsh命令接口
static void mp3_test(void)
{
    rt_thread_t test_thread = rt_thread_create(
        "mp3_test",
        mp3_player_test_thread_entry,
        RT_NULL,
        2048,
        10,
        5
    );
    
    if (test_thread) {
        rt_thread_startup(test_thread);
    } else {
        rt_kprintf("Failed to create test thread!\n");
    }
}
MSH_CMD_EXPORT(mp3_test, MP3 player test command);

int mp3player_example_init(void)
{
    rt_kprintf("MP3 Player Example Initialized\n");
    rt_kprintf("Use 'mp3_test' command to start the test\n");
    return RT_EOK;
}
INIT_APP_EXPORT(mp3player_example_init);